<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>serialize方法</title>
</head>

<body>
    <form id='form'>
        <input type="text" name='username'><br>
        <input type="password" name='password'><br>
        <input type="submit" value='提交'>
    </form>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $('#form').on('submit', function() {
                // 将表单内容拼接成字符串类型的参数
                // var params = $('#form').serialize();
                // console.log(params);// username=admin&password=123456
                serializeObject($(this));
                return false;
            })
            // 将表单中用户输入的内容转换为对象类型
        function serializeObject(obj) {
            var result = {};
            // {name: "username", value: "admin"}
            // {name: "password", value: "123456"}
            var params = obj.serializeArray();
            $.each(params, function(index, value) {
                    result[value.name] = value.value;
                })
                // console.log(result);//{username: "admin", password: "123456"}
            return result; //将处理的结果返回到函数外部
        }
    </script>
</body>

</html>